# -- coding: utf-8 --
"""
# MIAHX© BeamNG 车辆控制消息

**模块：MIAHX© / 框架基础服务层（FFST） / 交互框架信息表示模型（IFIRM）**

Version: 0.4.10,
Last updated: October 20, 2024

通知 BeamNG 实例，指定车辆对象将受到指定参数的控制。受到支持的参数包括：
- mode - DrivingMode - 驾驶模式：指定车辆将按照给定模式行驶

## Contributor(s)
* YAO Naiming <[naiming@iscas.ac.cn](mailto:naiming@iscas.ac.cn)>

## Change log
### v0.4.10
- .20: [优化] Reader 构造方法传入 topic_name
- .2: [优化] DrivingMode 枚举
### v0.4.6
- .10: [新增] 初始实现

---
Copyright © 2024-2025 MIAHX Contributors, ISCAS, and ASIC. All Rights Reserved.
Licensed under Apache-2.0.
"""

from enum import Enum

import miahx_fastdds as dds

from ..participant import Participant
from ..reader import Reader, ReaderListener
from ..topics import Topics
from ..writer import Writer


class EBngDrivingMode(Enum):
    """
    驾驶模式枚举
    """
    PARKING = dds.PARKING
    CUT_IN = dds.CUT_IN
    PARALLEL = dds.PARALLEL
    SLOW_AHEAD = dds.SLOW_AHEAD
    OBSTACLES_AHEAD = dds.OBSTACLES_AHEAD
    RUNNING_RED_LIGHT = dds.RUNNING_RED_LIGHT
    OPPOSITE = dds.OPPOSITE
    U_TURN = dds.U_TURN
    CHASING = dds.CHASING
    MERGING = dds.MERGING


class BngVehicleControlReader(Reader):
    """
    FastDDS 数据接收器，用于接收 BeamNG 车辆控制消息
    """

    def __init__(self, participant: Participant, on_data_listeners=None):
        topic_name = Topics.BNG_EFFECT
        topic_data_type = dds.BngVehicleControlMsgPubSubType()
        super().__init__(participant, topic_name, topic_data_type,
                         listener=ReaderListener(topic_name, topic_data_type, on_data_listeners))


class BngVehicleControlWriter(Writer):
    """
    FastDDS 数据写入器，用于写入 BeamNG 车辆控制消息
    """

    def __init__(self, participant: Participant):
        super().__init__(participant, Topics.BNG_EFFECT, dds.BngVehicleControlMsgPubSubType())

    def write(self, vid: str, mode: EBngDrivingMode):
        msg = dds.BngVehicleControlMsg()
        if vid:
            msg.vid(vid)
        msg.mode(mode.value)
        self._writer.write(msg)
